# -*- coding: utf-8 -*-
from .base import BaseTool
from .base import PrintUtils,CmdTask,FileUtils,AptUtils,ChooseTask
from .base import osversion,osarch,distribution
from .base import run_tool_file

class Tool(BaseTool):
    def __init__(self):
        self.type = BaseTool.TYPE_CONFIG
        self.name = "一键安装Qt-Creator(板卡端)"
        self.autor = 'hyw666'

    def install_qt_creator(self):
            #安装环境
            CmdTask("sudo apt update", 0).run()
            PrintUtils.print_info('Qt-Creator安装包较大,请耐心等待~')
            CmdTask("sudo apt install -y qtcreator", 0).run()
            PrintUtils.print_info('下面安装并配置Qt环境,请耐心等待~')
            CmdTask("sudo apt install -y qt5-default", 0).run()
            CmdTask("sudo apt install -y qttools5-dev-tools", 0).run()
            CmdTask("sudo apt install -y build-dep qtbase5-dev", 0).run()
            CmdTask("sudo apt install -y qtbase5-examples", 0).run()
            CmdTask("sudo apt install -y libudev-dev libinput-dev libts-dev libxcb-xinerama0-dev libxcb-xinerama0", 0).run()
            CmdTask("sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libx11-dev libsqlite0-dev libpq-dev libiodbc2-dev firebird-dev libgst-dev libxext-dev libxcb1 libxcb1-dev libx11-xcb1 libx11-xcb-dev", 0).run()
            CmdTask("sudo apt install -y libxcb-keysyms1 libxcb-keysyms1-dev libxcb-image0 libxcb-image0-dev libxcb-shm0 libxcb-shm0-dev", 0).run()
            CmdTask("sudo apt install -y libxcb-icccm4 libxcb-icccm4-dev libxcb-sync1 libxcb-sync-dev libxcb-render-util0 libxcb-render-util0-dev libxcb-xfixes0-dev libxrender-dev libxcb-shape0-dev libxcb-randr0-dev", 0).run() 
            CmdTask("sudo apt install -y libxcb-glx0-dev libxi-dev libdrm-dev libxcb-xinerama0 libxcb-xinerama0-dev libatspi2.0-dev libxcursor-dev libxcomposite-dev libxdamage-dev libxss-dev libxtst-dev libpci-dev libcap-dev", 0).run() 
            CmdTask("sudo apt install -y libxrandr-dev libdirectfb-dev libaudio-dev libxkbcommon-x11-dev", 0).run()

            CmdTask("sudo apt install -y gcc g++ gdbserver rsync cmake gdb", 0).run()

            #创建桌面图标
            # get home
            user =  FileUtils.getusers()[0]
            if user != 'root':
                home = "/home/{}".format(user)
            else:
                home = "/root"

            CmdTask("sudo touch  {}/Desktop/Qt-Creator.desktop".format(home), 0).run()
            CmdTask("sudo chmod 777 {}/Desktop/Qt-Creator.desktop".format(home), 0).run()
            if distribution=='ubuntu':
                CmdTask('echo "[Desktop Entry]\nVersion=1.0\nType=Application\nName=Qt-Creator\nComment=Qt-Creator\nExec=/usr/bin/qtcreator\nIcon=QtProject-qtcreator\nPath=/usr/lib/aarch64-linux-gnu/qtcreator\nTerminal=false\nStartupNotify=false" > {}/Desktop/Qt-Creator.desktop'.format(home), 0).run()
            else:
                CmdTask('echo "[Desktop Entry]\nVersion=1.0\nType=Application\nName=Qt-Creator\nComment=Qt-Creator\nExec=/usr/bin/qtcreator\nIcon=qtcreator\nPath=/usr/lib/aarch64-linux-gnu/qtcreator\nTerminal=false\nStartupNotify=false" > {}/Desktop/Qt-Creator.desktop'.format(home), 0).run()

            #复制例程
            CmdTask("mkdir -p {}/qt5/ ".format(home), 0).run()
            CmdTask("cp -rfd /usr/lib/aarch64-linux-gnu/qt5/examples/ {}/qt5/".format(home), 0).run()
            CmdTask("sudo chmod -R 777 {}/qt5/ ".format(home), 0).run()

            PrintUtils.print_info('已为您安装好Qt,请点击桌面图标打开或命令行输入"qtcreator"打开~')
            PrintUtils.print_info("Qt安装目录为/usr/lib/aarch64-linux-gnu/qt5,官方例程已复制到{}/qt5/examples,需要可自行打开运行测试~".format(home))
            
    def run(self):
        if osarch=='arm64':
            #正式的运行
            self.install_qt_creator()
        else:
            PrintUtils.print_info('不是arm64环境，如果是虚拟机安装,请选择对应的选项！')   
